package com.zy.portal.service.impl;

import java.util.HashMap;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.zy.common.util.HttpClientUtil;
import com.zy.common.util.JsonUtils;
import com.zy.common.util.TaotaoResult;
import com.zy.portal.pojo.Item;
import com.zy.portal.pojo.SearchResult;
import com.zy.portal.service.SearchService;

@Service
public class SearchServiceImpl implements SearchService {
	/**
	 * solr搜索基础url
	 */
	@Value("${SEARCH_BASE_URL}")
	private String SEARCH_BASE_URL;

	@Override
	public SearchResult search(String queryString, int page) {
		// 创建所需的map对象
		HashMap<String, String> param = new HashMap<>();
		param.put("q", queryString);
		param.put("page", page + ""); // “”变更成String类型
		try {
			/*
			 * 借助封装好的类，转换成String类型
			 */
			// http://localhost:8083/search/query
			String json = HttpClientUtil.doGet(SEARCH_BASE_URL, param);
			// 将字符串转换成对象
			TaotaoResult taotaoResult = TaotaoResult.formatToPojo(json, SearchResult.class);
			if (taotaoResult.getStatus() == 200) {
				//得到响应中的数据
				SearchResult result = (SearchResult) taotaoResult.getData();
				return result;
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return null;
	}

}
